package ooday04;

//多态的演示
public class Polymorphic {
    public static void main(String[] args) {
        /*
        Dog o =new Dog("小黑",1,"黑");
        //o1.name/age/color/drink()/eat()/swim()/lookHome();

        Animal o1 = new Dog("小黑",1,"黑");
        //o2.name/age/color/drink()/eat();

        Swim o2 = new Dog("小黑",2,"黑");
        //o3.swim();

         */

        /*
        Master master = new Master();
        Animal dog = new Dog("小黑",1,"黑");

        Chick chick = new Chick("小黄",2,"黄");

        Fish fi = new Fish("小红",3,"红");霏闹

        master.feed(dog);
        master.feed(chick);
        master.feed(fi);

         */
        //向下造型的第二个引用
        Animal[] animal = new Animal[5];//创建一个引用类型的数组,存放每一个动物
        animal[0] = new Dog("小黑", 1, "黑");
        animal[1] = new Dog("大黑", 2, "黑");
        animal[2] = new Fish("小金", 4, "金");
        animal[3] = new Fish("小红", 3, "红");
        animal[4] = new Chick("小灰", 1, "灰");
        for (int i = 0; i < animal.length; i++) {//编译每一个动物
            System.out.println(animal[i].name);//输出每一个动物的名字
            animal[i].drink();//每一个动物喝水
            animal[i].eat();//每一个动物吃饭

            if (animal[i] instanceof Dog) {
                Dog dog = (Dog) animal[i];
                dog.lookHome();
            }
            if (animal[i] instanceof Chick) {
                Chick chick = (Chick) animal[i];
                chick.layEgg();
            }
            if (animal[i] instanceof Swim) {//这个接口是实现所有连接了Swim接口的派生类,Swim swim = new (Dog)[i]或者(Fish)[i];
                Swim swim = (Swim) animal[i];
                swim.swim();
            }
        }


        /*
        Animal o =new Dog("小黑",1,"黑");
        Dog g = (Dog)o;
        Swim s = (Swim)o;

        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof  Fish);

        if (o instanceof Fish){
            Dog f = (Dog)o;
            System.out.println(f.age);
        }

         */


    }
}
